import 'package:flutter/material.dart';
import 'package:suburban_inside_flutter/viewmodel/general_viewmodel.dart';
import 'package:suburban_inside_flutter/widget/colorful_button.dart';
import 'package:suburban_inside_flutter/widget/page_wrapper.dart';


class SaveHistoryPage extends StatefulWidget {
  const SaveHistoryPage({super.key});

  @override
  State<SaveHistoryPage> createState() => _SaveHistoryPageState();
}

class _SaveHistoryPageState extends State<SaveHistoryPage> {
  final _controller = TextEditingController();

  void handelSubmit(String text) {
    text = text.trim();
    if (text.isNotEmpty) {

    }
    else {
      print("submit - blank submit!");
      _controller.clear();
    }

    text = text.replaceAll(" ", "_");
    text = text.replaceAll("\t", "_");
    print("submit: '$text'");
    GeneralViewModel().pushRequest("save $text");
    Navigator.pop(context);
  }

  late final String defaultString;

  @override
  void initState() {
    super.initState();
    defaultString = "${DateTime.timestamp().millisecondsSinceEpoch}";
    _controller.text = defaultString;
  }

  @override
  Widget build(BuildContext context) {
    return PageWrapper(
      outerLRPadding: 300,
      outerTBPadding: 185,
      title: "Save Actions",
      child: Column(
        children: [
          TextField(
            autofocus: true,
            controller: _controller,
            textAlign: TextAlign.center, // 文字居中
            style: const TextStyle(color: Colors.white, fontSize: 18), // 文字样式
            decoration: InputDecoration(
              hintText: "set name",
              hoverColor: Colors.blue.shade100, // 悬停时的颜色
              focusColor: Colors.blue, // 聚焦时的颜色
              enabledBorder: const UnderlineInputBorder(
                borderSide: BorderSide(color: Colors.grey, width: 1.0), // 默认下划线颜色
              ),
              focusedBorder: const UnderlineInputBorder(
                borderSide: BorderSide(color: Colors.blue, width: 2.0), // 聚焦时的下划线颜色
              ),
              hintStyle: const TextStyle(color: Colors.white38, fontSize: 18, fontWeight: FontWeight.w300), // 默认文字颜色
            ),
            onEditingComplete: () => handelSubmit(_controller.text),
          ),

          const Expanded(child: SizedBox.shrink()),

          Align(
            alignment: Alignment.centerRight,
            child: ColorfulButton(
              height: 36,
              paddingLR: 30,
              style: const TextStyle(height: 2.3),
              fgColor: Colors.white,
              bgColor: Colors.black54,
              fgColorHover: Colors.white,
              bgColorHover: Colors.lightBlueAccent,
              onTap: () => handelSubmit(_controller.text),
              child: const Text("apply"),
            ),
          )
        ],
      )
    );
  }
}
